package com.xdhl.fu.fim.price.strategy;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.utils.MathUtils;
import com.xdhl.fu.fim.service.IFlightDiscountService;
import com.xdhl.fu.flight.info.api.domain.FlightDiscount;
import com.xdhl.fu.flight.info.api.domain.FlightInfo;
import com.xdhl.fu.flight.info.api.domain.FlightSchedule;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 航班折扣价格策略
 *
 * @author 朱力维
 * @time 2022/6/9 15:59
 */
@Component
public class FlightClassDiscountStrategy implements PriceCalcStrategy {

    @Resource
    private IFlightDiscountService flightDiscountService;

    private FlightDiscount flightDiscount;

    @Override
    public int order() {
        return 3;
    }

    @Override
    public PriceCalcStrategy calcEconomy(FlightInfo flightInfo, FlightSchedule schedule) {
        Double oldPrice = schedule.getEconomyPrice();
        Double discount = flightDiscount.getEconomyDiscount();
        Double newPrice = MathUtils.priceMultip(oldPrice, discount);
        schedule.setEconomyPrice(newPrice);
        return this;
    }

    @Override
    public PriceCalcStrategy calcBusiness(FlightInfo flightInfo, FlightSchedule schedule) {
        Double oldPrice = schedule.getBusinessPrice();
        Double discount = flightDiscount.getBusinessDiscount();
        Double newPrice = MathUtils.priceMultip(oldPrice, discount);
        schedule.setBusinessPrice(newPrice);
        return this;
    }

    @Override
    public PriceCalcStrategy calcHeader(FlightInfo flightInfo, FlightSchedule schedule) {
        Double oldPrice = schedule.getHeadPrice();
        Double discount = flightDiscount.getHeadDiscount();
        Double newPrice = MathUtils.priceMultip(oldPrice, discount);
        schedule.setHeadPrice(newPrice);
        return this;
    }

    @Override
    public boolean enable(FlightInfo flightInfo, FlightSchedule schedule) {
        LambdaQueryWrapper<FlightDiscount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlightDiscount::getFlightDataId, flightInfo.getDataId());
        this.flightDiscount = flightDiscountService.getOne(wrapper);
        return null != this.flightDiscount;
    }

}
